<?php

class Module_User_Administrator extends Module_BaseModule
{
    use Lib_BaseTraitsModule;
    private static $obj = null;
    private $model = null;
    // 状态数组
    public static $statusArr = [
        '1' => '启用',
        '2' => '禁用',
    ];
    /**
     * 封闭构造
     */
    public function __construct()
    {
        $this->model = new Model_User_Administrator();
    }

    /**
     * 单例获取
     * 保证一条进程只产生一个Module对象
     */
    public static function getInstance()
    {
        if (empty(self::$obj)) {
            self::$obj = new self();
        }
        return self::$obj;
    }

    public function getWorkerd($line){
        $list = Module_Mediate_Workernumber::getInstance()->getAllBySearch(['workerId'], ["where" => ["isDel=1","line={$line}"]]);
        $list = array_column($list, 'workerId');
        return $list;
    }
    /**
     * 查询所有
     *
     * @return array
     */
    public function getListAll(): array
    {
        return $this->model->getListAll();
    }

    /**
     * @param $mobile
     * @return array
     */
    public function getByMobile($mobile)
    {
        return $this->model->getByMobile($mobile);
    }

    /**
     * @param $jobNumber
     * @return mixed
     */
    public function getByJobNumber($jobNumber)
    {
        return $this->model->getByJobNumber($jobNumber);
    }



    /**
     * @param $mobile
     * @return array
     */
    public function getById($id = 0)
    {
        if (!$id) {
            return [];
        }
        return $this->model->getById($id);
    }

    /**
     * @param int $role_id
     * @param int $status
     * @return array
     */
    public function getByRoleId($role_id = 0, $status = 0)
    {
        if (!$role_id) {
            return [];
        }
        return $this->model->getByRoleId($role_id, $status);
    }

    /**
     * @param $mobile
     * @param $pwd
     * @return array
     */
    public function getAdminByMobilePwd($mobile, $pwd)
    {
        $admin = $this->model->getByMobile($mobile);
        if ($admin) {
            $admin = $this->model->getAdminByMobilePwd($mobile, self::encodePassword($pwd, $admin['register_time']));
            if ($admin) {
                return $admin;
            }
        }
        return [];
    }

    /**
     * @param $id
     * @param $randomKey
     * @return mixed
     */
    public function updateRandomKeyById($id, $randomKey)
    {
        return $this->model->updateRandomKeyById($id, $randomKey);
    }

    /**
     * @param $password
     * @param $registerTime
     * @return string
     */
    public static function encodePassword($password, $registerTime)
    {
        $pass = md5($password);
        $time = md5($registerTime);
        return substr($pass, 24, 8) . substr($time, 0, 24);
    }

    /**
     * @param array $fields
     * @param array $search
     * @param bool $cache
     * @return mixed
     */
    public function getAllBySearch($fields = array(), $search = array(), $cache = false)
    {
        return $this->model->getAllBySearch($fields, $search, $cache);
    }

    /*
     * 存储数据
     */
    public function insertData($saveData)
    {
        return $this->model->insertData($saveData);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getAdminById($id)
    {
        return $this->model->getAdminById($id);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function deleteById($id)
    {
        return $this->model->deleteById($id);
    }

    /**
     * @param $updateData
     * @return mixed
     */
    public function updateItemById($updateData)
    {
        return $this->model->updateItemById($updateData);
    }

    /**
     * @param $id
     * @param $pwd
     * @return mixed
     */
    public function updatePwdById($id, $pwd)
    {
        return $this->model->updatePwdById($id, $pwd);
    }

    /**
     * @param $role_id
     * @return mixed
     */
    public function getAdminByRoleId($role_id)
    {
        return $this->model->getAdminByRoleId($role_id);
    }
}
